#ifndef __RTC_H__
#define __RTC_H__

#define I2C_USED            I2C1
#define I2C_CLK             RCC_APB1Periph_I2C1
#define I2C_CLOCK_HZ        100000

#define I2C_GPIO_PORT       GPIOB
#define I2C_GPIO_CLK        RCC_APB2Periph_GPIOB
#define I2C_SCL_PIN         GPIO_Pin_6
#define I2C_SDA_PIN         GPIO_Pin_7



#define DS1307_ADDR         0x68
#define SEC_REG             0x00
#define MIN_REG             0x01
#define HOUR_REG            0x02
#define DAY_REG             0x03
#define DATE_REG            0x04
#define MONTH_REG           0x05
#define YEAR_REG            0x06
#define CONTROL_REG         0x07
#define RAM_MEM             0x08
#define RAM_MEM_SIZE        0xFF
#define RTC_RETRY           100

#define HALT                (1 << 7)
#define MODE12_24           (1 << 6)
#define PM                  (1 << 5)
#define TIME_SIZE           7

enum rtc_mode {
    MODE12      = 0,
    MODE24      = 1
};

struct xrtc_time {
    enum rtc_mode mode;
    unsigned char seconds;  /* 00-59 */
    unsigned char minutes;  /* 00-59 */
    unsigned char hours;    /* 1-12 + AM/PM or 00-23 */
    unsigned char day;      /* 01-07 */
    unsigned char date;     /* 01-31 */
    unsigned char month;    /* 01-12 */
    unsigned long year_offset;
    unsigned long year;     /* 00-99 */
    unsigned char am_pm;
    error_callback_t    rtc_error_handler;
};

#ifdef __cplusplus
extern "C" {
#endif

extern void rtc_open(struct xrtc_time *time);
extern unsigned char rtc_gettime(struct xrtc_time *time);
extern unsigned char rtc_settime(struct xrtc_time *time);

extern void rtc_setsecond(unsigned char second);
extern void rtc_setminute(unsigned char minute);
extern void rtc_sethour(unsigned char hour);
extern void rtc_setdate(unsigned char date);
extern void rtc_setday(unsigned char day);
extern void rtc_setmonth(unsigned char month);
extern void rtc_setyear(unsigned char year);
extern void rtc_setmode(enum rtc_mode mode);

extern void rtc_close(void);

#ifdef __cplusplus
}
#endif

#endif